package com.yztc.kuaikanmanhua.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yztc.kuaikanmanhua.R;
import com.yztc.kuaikanmanhua.fragment.CommunityFragment;
import com.yztc.kuaikanmanhua.fragment.DiscoverFragment;
import com.yztc.kuaikanmanhua.fragment.ManhuaFragment;
import com.yztc.kuaikanmanhua.fragment.MeFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 此类是总的控制界面
 */
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup rgMenu;//用于控制底部单选按钮的RadioGroup
    private ManhuaFragment manhuaFragment;//漫画首页碎片
    private DiscoverFragment discoverFragment;//发现碎片
    private CommunityFragment communityFragment;//社区碎片
    private MeFragment meFragment;//我的碎片
    private Fragment isFragment;//记录正在使用的碎片
    private RadioButton rbManhua;//漫画的单选按钮，需要改变字体的颜色
    private RadioButton rbDiscover;//漫画的单选按钮，需要改变字体的颜色
    private RadioButton rbShequ;//漫画的单选按钮，需要改变字体的颜色
    private RadioButton rbMe;//漫画的单选按钮，需要改变字体的颜色
    private List<RadioButton> buttons;//用于存放单选按钮，需要改变字体颜色

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        //初始化控件
        initView();
        //初始化碎片
        initFragment(savedInstanceState);
        //实现碎片间的切换
        rgMenu.setOnCheckedChangeListener(this);
    }

    /**
     * 为页面加载初始状态的fragment
     * @param savedInstanceState
     */
    public void initFragment(Bundle savedInstanceState){
        //判断activity是否重建，如果不是，则不需要重新建立fragment.
        if (savedInstanceState == null) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            if (manhuaFragment == null) {
                manhuaFragment = new ManhuaFragment();
            }
            isFragment = manhuaFragment;
            ft.replace(R.id.ll_total_container,manhuaFragment).commit();
        }
    }

    //初始化控件和按钮文字的集合
    public void initView() {
        rgMenu = (RadioGroup) findViewById(R.id.rg_menu);
        rbManhua = (RadioButton) findViewById(R.id.rb_manhua);
        rbDiscover = (RadioButton) findViewById(R.id.rb_discover);
        rbShequ = (RadioButton) findViewById(R.id.rb_shequ);
        rbMe = (RadioButton) findViewById(R.id.rb_me);
        buttons = new ArrayList<>();
        buttons.add(rbManhua);
        buttons.add(rbDiscover);
        buttons.add(rbShequ);
        buttons.add(rbMe);
    }

    //单选按钮的监听事件，用于切换fragment
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_manhua:
                if (manhuaFragment == null) {
                    manhuaFragment = new ManhuaFragment();
                }
                switchContent(isFragment,manhuaFragment);
                changeColor();
                buttons.get(0).setTextColor(getResources().getColor(R.color.colorRadioButton));
                break;
            case R.id.rb_discover:
                if (discoverFragment == null) {
                    discoverFragment = new DiscoverFragment();
                }
                switchContent(isFragment,discoverFragment);
                changeColor();
                buttons.get(1).setTextColor(getResources().getColor(R.color.colorRadioButton));
                break;
            case R.id.rb_shequ:
                if (communityFragment == null) {
                    communityFragment = new CommunityFragment();
                }
                switchContent(isFragment,communityFragment);
               changeColor();
                buttons.get(2).setTextColor(getResources().getColor(R.color.colorRadioButton));
                break;
            case R.id.rb_me:
                if (meFragment == null) {
                    meFragment = new MeFragment();
                }
                switchContent(isFragment,meFragment);
                changeColor();
                buttons.get(3).setTextColor(getResources().getColor(R.color.colorRadioButton));
                break;
        }

    }

    //改变单选按钮文字的颜色
    public void changeColor() {
        for (int j = 0; j < buttons.size(); j++) {
            buttons.get(j).setTextColor(Color.BLACK);
        }
    }

    /**
     * 当fragment进行切换时，采用隐藏与显示的方法加载fragment以防止数据的重复加载
     * @param from
     * @param to
     */
    public void switchContent(Fragment from, Fragment to) {
        if (isFragment != to) {
            isFragment = to;
            FragmentManager fm = getSupportFragmentManager();
            //添加渐隐渐现的动画
            FragmentTransaction ft = fm.beginTransaction();
            // 先判断是否被add过
            if (!to.isAdded()) {
                // 隐藏当前的fragment，add下一个到Activity中
                ft.hide(from).add(R.id.ll_total_container,to).commit();
            } else {
                // 隐藏当前的fragment，显示下一个
                ft.hide(from).show(to).commit();
            }
        }
    }
}
